package com.jiankangjin.weixinchannel.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.weixinchannel.config.WeixinConfig;
import com.jiankangjin.weixinchannel.util.MD5Util;
import com.jiankangjin.weixinchannel.util.Sha1Util;
import com.jiankangjin.weixinchannel.util.TenpayHttpClient;
import com.jiankangjin.weixinchannel.util.XMLUtil;

/**
 * 对账单接口 对账单接口:	/download_bill
 * 对应微信接口：		https://api.mch.weixin.qq.com/pay/downloadbill
 * 微信支付v3.3.5版本
 */
@Controller
public class DownloadbillController {
	private static final transient Log logger = LogFactory.getLog(DownloadbillController.class);

	/**
	 * 测试结果
	 * {return_msg=missing parameter, return_code=FAIL}
	 */
	public static void main(String[] args) {
		new DownloadbillController().downloadbill("20141004", "ALL", "", "");
	}
	
	/**
	 * 对账单接口 对账单接口
	 * 商户可以通过该接口下载历史交易清单。某些情况，比如掉单、交易错误，导致商户侧和微信侧数据不一致，通过对账单核对后可校正支付状态。
	 */
	@RequestMapping(value="/download_bill")
	@ResponseBody
	public Map<String, Object> downloadbill(
			@RequestParam(required = false) String bill_date,
			@RequestParam(required = false) String bill_type,
			@RequestParam(required = false) String sub_mch_id,
			@RequestParam(required = false) String device_info) {
		StringBuilder sb = new StringBuilder();
		sb.append("bill_data=").append(bill_date);
		sb.append(" bill_type=").append(bill_type);
		logger.info("对账单下载接口:" + sb);
		if (bill_date != null && bill_date.length() != 8) {
			logger.error("bill_date错误");
			return null;
		}
		if (!bill_type.equals("ALL") && !bill_type.equals("SUCCESS") && !bill_type.equals("REFUND")){
			logger.error("bill_type错误");
			return null;
		}
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("appid", WeixinConfig.APP_ID);
			params.put("mch_id", WeixinConfig.MCH_ID);
			params.put("sub_mch_id", sub_mch_id);// 子商户号
			params.put("device_info", "device_info");// 微信支付分配的终端设备号
			params.put("nonce_str", Sha1Util.getNonceStr());// 随机串
			params.put("bill_date", bill_date);// 对账单日起
			params.put("bill_type", bill_type);// 账单类型 ALL SUCCESS REFUND
			Map<String, Object> sPara = MD5Util.MD5Encode(params, "utf8");
			String xmlStr = XMLUtil.toXMLstr(sPara);
	    	logger.info("调微信对账单下载接口:"+xmlStr);
			TenpayHttpClient httpClient = new TenpayHttpClient();
			String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	    	if(httpClient.callHttpPost(url, xmlStr)) {
		    	Map<String, String> weixinMap = XMLUtil.doXMLParse(new String(httpClient.getResContent().getBytes("gbk"), "utf8"));
				logger.info("微信对账单下载接口返回:"+weixinMap);
	    	}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
